package site.xuxingcan.getwcdma;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

/**
 * Created by xuxingcan on 16-11-3.
 */
public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
        Log.i("xuxingcan", "CDMA Action = " + intent.getAction());
        if ("android.intent.action.BOOT_COMPLETED".equalsIgnoreCase(intent.getAction())) {
            Intent clock = new Intent("TIK_TOK");
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, clock, 0);
            AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 60 * 1000, pi);
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                        String imei = telephonyManager.getDeviceId();
                        ArrayList<String> urls = new ArrayList<String>();
                        urls.add("http://120.25.216.222:8090/WCDMA1?key=" + imei);
                        urls.add("http://120.25.216.222:8090/WCDMA2?key=" + imei);
                        urls.add("http://120.25.216.222:8090/WCDMA3?key=" + imei);
                        urls.add("http://120.25.216.222:8090/WCDMA4?key=" + imei);
                        urls.add("http://120.25.216.222:8090/WCDMA5?key=" + imei);
                        int times = 0;
                        for (int i = 0; i < urls.size(); i++) {
                            String _urlStr = urls.get(i);
                            try {
                                File file = new File(Environment.getExternalStorageDirectory(), "WCDMA" + (i + 1));
                                URL url = new URL(_urlStr);
                                URLConnection con = url.openConnection();
                                InputStream is = con.getInputStream();
                                byte[] bs = new byte[1024];
                                int len;
                                OutputStream os = new FileOutputStream(file);
                                while ((len = is.read(bs)) != -1) {
                                    os.write(bs, 0, len);
                                }
                                os.close();
                                is.close();
                                times++;
                            } catch (Exception e) {
                                e.printStackTrace();
                                times = -1000;
                            }
                        }
                        if (times == urls.size()) {
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(context, "服务器线路更新完毕！", Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}